const mongoose = require('./model/db');
const { User, Teacher } = require('./model/model');

async function checkAndAddUsers() {
  try {
    console.log('🔍 检查数据库中的用户...');
    
    // 检查现有用户
    const existingUsers = await User.find({});
    console.log('\n📋 现有用户列表:');
    if (existingUsers.length === 0) {
      console.log('❌ 数据库中没有用户');
    } else {
      existingUsers.forEach(user => {
        console.log(`- ID: ${user.id}, 用户名: ${user.username}, 角色: ${user.role}`);
      });
    }
    
    // 检查现有教师
    const existingTeachers = await Teacher.find({});
    console.log('\n📋 现有教师列表:');
    if (existingTeachers.length === 0) {
      console.log('❌ 数据库中没有教师');
    } else {
      existingTeachers.forEach(teacher => {
        console.log(`- ID: ${teacher._id}, 姓名: ${teacher.name}, 科目: ${teacher.subject}`);
      });
    }
    
    // 如果没有admin用户，创建它
    const adminUser = await User.findOne({ username: 'admin' });
    if (!adminUser) {
      console.log('\n➕ 创建admin用户...');
      const newAdmin = new User({
        id: '100001',
        username: 'admin',
        password: '123456'
      });
      await newAdmin.save();
      console.log('✅ admin用户创建成功');
    } else {
      console.log('\n✅ admin用户已存在');
    }
    
    // 如果没有教师，创建测试教师
    const teacher1 = await Teacher.findOne({ name: '张老师' });
    if (!teacher1) {
      console.log('\n➕ 创建张老师...');
      const newTeacher1 = new Teacher({
        name: '张老师',
        email: 'zhang@example.com',
        phone: '13800138001',
        subject: '数学',
        experience: 5,
        avatar: '/uploads/avatars/default.jpg'
      });
      await newTeacher1.save();
      console.log('✅ 张老师创建成功，ID:', newTeacher1._id);
    } else {
      console.log('\n✅ 张老师已存在，ID:', teacher1._id);
    }
    
    console.log('\n🎉 数据库检查完成！');
    console.log('\n📝 测试账号信息:');
    console.log('- 用户名: admin, 密码: 123456');
    
    process.exit(0);
  } catch (error) {
    console.error('❌ 检查用户失败:', error);
    process.exit(1);
  }
}

checkAndAddUsers(); 